﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Web.Configuration;

/// <summary>
/// Summary description for Mural
/// </summary>
public class Mural : Banco
{
    private string Descricao, Titulo;
    public Mural()
    { }
    public Mural(string Titulo, string Descricao)
    {
        this.Descricao = Descricao;
        this.Titulo = Titulo;
    }
    /// <summary>
    /// Salva no banco uma noticia para o mural.
    /// </summary>
    /// <returns>True para ok, false para erro no banco.</returns>
    public bool Salvar()
    {
        string Hora = DateTime.Now.ToString("HH:mm:ss");
        string Data = DateTime.Now.ToString("yyyy/MM/dd");
        string Login = HttpContext.Current.User.Identity.Name.ToString();
        string IP = HttpContext.Current.Request.UserHostAddress.ToString();
        return Comando("INSERT INTO mural (Titulo, Descricao, Login, Data, Hora, IP) VALUES ('" + Titulo + "', '" + Descricao + "', '" + Login + "', '" + Data + "', '" + Hora + "', '" + IP + "')");
    }
    /// <summary>
    /// Altera noticia no banco.
    /// </summary>
    /// <param name="Codigo">Codigo da noticia a ser alterada.</param>
    /// <returns>True para ok, false para erro no banco.</returns>
    public bool Alterar(string Codigo)
    {
        string Hora = DateTime.Now.ToString("HH:mm:ss");
        string Data = DateTime.Now.ToString("yyyy/MM/dd");
        string Login = HttpContext.Current.User.Identity.Name.ToString();
        string IP = HttpContext.Current.Request.UserHostAddress.ToString();
        return Comando("UPDATE mural SET Titulo = '" + Titulo + "', Descricao ='" + Descricao + "', Login = '" + Login + "', Data = '" + Data + "', Hora ='" + Hora + "', IP ='" + IP + "' WHERE (Codigo = '" + Codigo + "')");
    }    
    /// <summary>
    /// Apaga ajuda no banco.
    /// </summary>
    /// <param name="Codigo">Codigo da ajuda a ser apagada.</param>
    /// <returns>True para ok, false para erro no banco.</returns>
    public bool Apagar(string Codigo)
    {
        return Comando("DELETE FROM mural WHERE (Codigo = " + Codigo + ")");
    }
    /// <summary>
    /// Buscar Mural para alterar.
    /// </summary>
    /// <param name="Codigo">Codigo do mural para alterar.</param>
    /// <returns>vetor com mural</returns>
    public string[] BuscarMural(string Codigo)
    {
        string[] Lista = new string[3];
        string sql = "SELECT Codigo, Titulo, Descricao FROM Mural WHERE (Codigo = " + Codigo + ")";

        MySqlConnection conection = new MySqlConnection(WebConfigurationManager.ConnectionStrings["chamados"].ConnectionString);
        MySqlCommand comando = new MySqlCommand(sql, conection);

        MySqlDataReader dtreader;
        conection.Open();
        dtreader = comando.ExecuteReader();

        if (dtreader.HasRows)
        {
            while (dtreader.Read())
            {
                Lista[0] = dtreader.GetValue(0).ToString();
                Lista[1] = dtreader.GetValue(1).ToString();
                Lista[2] = dtreader.GetValue(1).ToString();
            }
        }
        conection.Close();
        return Lista;
    }
}